package com.zjs.demo;

import com.zjs.demo.enmus.Fruit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * @author zjs1
 * 超市类
 */
@Data
@Slf4j
public class Supermarket {

    /**
     * 草莓特价开始时间
     */
    private LocalDateTime startTime;

    /**
     * 草莓结束特价时间
     */
    private LocalDateTime endTime;

    public static void main(String[] args) throws Exception {
        Supermarket supermarket = new Supermarket();
        /*
         * 苹果 8 元/斤，草莓 13 元/斤 苹果 8 元/斤，芒果，20 元/斤
         * */
        // 题目一
        System.out.println("题目一总价: " + supermarket.computerTotalPrice(4, 4)); // 总价 84.00

        // 题目二
        System.out.println("题目二总价: " + supermarket.computerTotalPrice(4, 4, 2, false, false)); // 总价 124.00

        // 题目三：草莓限时折扣
        supermarket.setStartTime(LocalDateTime.of(2024, 11, 22, 0, 0, 0));
        supermarket.setEndTime(LocalDateTime.of(2024, 11, 30, 12, 0, 0));
        System.out.println("题目三草莓限时折扣：总价: " + supermarket.computerTotalPrice(4, 4, 2, true, false)); // 总价 113.60

        // 题目四：草莓限时折扣 + 满减
        System.out.println("题目四草莓限时折扣 + 满减：总价: " + supermarket.computerTotalPrice(4, 4, 2, true, true)); // 总价 103.60
    }


    /**
     * 题目一
     *
     * @param appleWeight      苹果重量
     * @param strawberryWeight 草莓重量
     * @return 总价
     */
    public BigDecimal computerTotalPrice(int appleWeight, int strawberryWeight) throws Exception {
        // 校验重量
        verifyWeight(Fruit.APPLE.getName(), appleWeight);
        verifyWeight(Fruit.STRAWBERRY.getName(), strawberryWeight);

        // 计算价格
        BigDecimal totalPrice = Fruit.APPLE.computerPrice(appleWeight).add(Fruit.STRAWBERRY.computerPrice(strawberryWeight));
        return totalPrice.setScale(2, RoundingMode.HALF_UP);
    }


    /**
     * 题目二：strawberryDiscountEnable = false；maxOutEnable = false
     * 题目三（草莓折扣）：strawberryDiscountEnable = true; maxOutEnable = false
     * 题目四（草莓折扣 + 满减）：strawberryDiscountEnable = true; maxOutEnable = true
     *
     * @param appleWeight              苹果重量
     * @param strawberryWeight         草莓重量
     * @param mangoWeight              芒果重量重量
     * @param strawberryDiscountEnable 草莓折扣开关
     * @param maxOutEnable             满减开关
     * @return 总价
     */
    public BigDecimal computerTotalPrice(int appleWeight, int strawberryWeight, int mangoWeight, boolean strawberryDiscountEnable, boolean maxOutEnable) throws Exception {
        // 校验重量
        verifyWeight(Fruit.APPLE.getName(), appleWeight);
        verifyWeight(Fruit.STRAWBERRY.getName(), strawberryWeight);
        verifyWeight(Fruit.MANGO.getName(), mangoWeight);

        BigDecimal totalPrice = Fruit.APPLE.computerPrice(appleWeight).add(Fruit.MANGO.computerPrice(mangoWeight));

        // 计算草莓价格
        BigDecimal strawberryPrice = Fruit.STRAWBERRY.computerPrice(strawberryWeight);
        LocalDateTime now = LocalDateTime.now();

        // 草莓限时八折
        if (strawberryDiscountEnable && now.isBefore(endTime) && now.isAfter(startTime)) {
            strawberryPrice = strawberryPrice.multiply(new BigDecimal("0.8"));
        }
        totalPrice = totalPrice.add(strawberryPrice);

        // 满减
        if (maxOutEnable && totalPrice.compareTo(new BigDecimal("100")) >= 0) {
            totalPrice = totalPrice.subtract(new BigDecimal("10"));
        }
        return totalPrice.setScale(2, RoundingMode.HALF_UP);  // 保留两位小数
    }

    /**
     * 校验水果重量为大于0的整数
     *
     * @param name   水果名称
     * @param weight 水果重量
     * @throws Exception
     */
    private void verifyWeight(String name, int weight) throws Exception {
        if (weight <= 0) {
            throw new Exception("重量必须大于零。水果: " + name + "，无效的重量: " + weight);
        }
    }
}
